package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domin.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private static StudentService studentService = new StudentService();
    private static Scanner sc = new Scanner(System.in);

    //入口
    public void start() {
        loop:
        while (true) {
            System.out.println("--------欢迎来到<学生>管理系统------");
            System.out.println("请输入您的选择：1 添加学生 2 删除学生 3 修改学生 4 查看学生 5 退出系统 ");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudnet();
                    break;
                case "5":
                    System.out.println("感谢您的使用，再见");
                    break loop;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    //查看学生
    public void findAllStudnet() {
        Student[] stus = studentService.findAllStudent();
        if (stus == null) {
            System.out.println("没有数据，请先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getSid() + stu.getName() + stu.getAge() + stu.getBirthday());
            }
        }
    }

    //删除学生
    private void deleteStudent() {
        String sid = luruxuehao();
        studentService.deleteStudent(sid);
        System.out.println("删除成功");
    }

    //修改学生
    private void updateStudent() {
        String sid = luruxuehao();
        Student stu = luruxuehaoInfo(sid);
        studentService.updateStudent(sid,stu);
        System.out.println("修改成功");

    }

        public String luruxuehao(){
            System.out.println("请输入学号");
            String sid = null;
            while (true) {
                sid = sc.next();
                //判断学号是否存在
                boolean exists = studentService.isExists(sid);
                if (exists) {
                   break;
                } else {
                    System.out.println("已经存在，请重新输入");
                }
            }
            return sid;
        }

//添加学生
        public void addStudent(){
            String sid = null;
            while (true) {
                System.out.println("请输入学号");
                sid =sc.next();
                //判断学号是否存在
                boolean exists = studentService.isExists(sid);
                if (exists) {
                    System.out.println("学号已被占用");
                } else {
                    break;
                }
            }
            Student stu = luruxuehaoInfo(sid);
            boolean b = studentService.addStudent(stu);
            if (b) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
        }

    private Student luruxuehaoInfo(String sid) {
        System.out.println("请输入姓名");
        String name = StudentController.sc.next();
        System.out.println("请输入年龄");
        String age = StudentController.sc.next();
        System.out.println("请输入生日");
        String birthday = StudentController.sc.next();
        Student stu = new Student();
        stu.setSid(sid);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }
}
